<?php
/**
 * Final Model Test Script
 * Properly loads webman environment and tests all models
 */

// Define base paths
define('BASE_PATH', dirname(__FILE__));
define('WEBMAN_PATH', BASE_PATH);

// Load composer autoloader
require_once BASE_PATH . '/vendor/autoload.php';

// Load webman bootstrap
require_once BASE_PATH . '/support/bootstrap.php';

echo "🧪 Testing Model Database Connections with Webman Environment...\n\n";

// Models to test
$models = [
    'app\common\model\Device' => 'gf_device',
    'app\common\model\ChannelStats' => 'gf_channel_stats',
    'app\common\model\AttributionEvent' => 'gf_attribution_event',
    'app\common\model\AttributionInstall' => 'gf_attribution_install',
    'app\common\model\AttributionTrack' => 'gf_attribution_track',
];

$successCount = 0;
$failedCount = 0;
$errors = [];

foreach ($models as $modelClass => $expectedTable) {
    echo "Testing: $modelClass\n";
    echo "Expected table: $expectedTable\n";
    
    try {
        // Check if class exists
        if (!class_exists($modelClass)) {
            echo "  ❌ Class not found: $modelClass\n";
            $failedCount++;
            $errors[] = "$modelClass: Class not found";
            continue;
        }
        
        // Get table name from model
        try {
            $reflection = new ReflectionClass($modelClass);
            if ($reflection->hasProperty('table')) {
                $property = $reflection->getProperty('table');
                $property->setAccessible(true);
                
                // Create instance to get table name
                $instance = $reflection->newInstance();
                $actualTable = $property->getValue($instance);
                
                echo "  Actual table: $actualTable\n";
                
                if ($actualTable === $expectedTable) {
                    echo "  ✅ Table name matches expected value\n";
                } else {
                    echo "  ⚠️  Table name mismatch: expected $expectedTable, got $actualTable\n";
                }
            } else {
                echo "  ⚠️  No 'table' property found\n";
            }
        } catch (Exception $e) {
            echo "  ⚠️  Could not get table name: " . $e->getMessage() . "\n";
        }
        
        // Test database connection using the model
        try {
            $instance = new $modelClass();
            $query = $instance->limit(1)->select();
            echo "  ✅ Database query successful\n";
            $successCount++;
        } catch (Exception $e) {
            echo "  ❌ Database query failed: " . $e->getMessage() . "\n";
            $failedCount++;
            $errors[] = "$modelClass: Database query failed - " . $e->getMessage();
        }
        
    } catch (Exception $e) {
        echo "  ❌ Unexpected error: " . $e->getMessage() . "\n";
        $failedCount++;
        $errors[] = "$modelClass: Unexpected error - " . $e->getMessage();
    }
    
    echo "\n";
}

echo "📊 TEST RESULTS:\n";
echo "==================================================\n";
echo "Total models tested: " . count($models) . "\n";
echo "✅ Successful: $successCount\n";
echo "❌ Failed: $failedCount\n";
echo "Success rate: " . round(($successCount / count($models)) * 100, 2) . "%\n\n";

if ($failedCount > 0) {
    echo "❌ ERRORS ENCOUNTERED:\n";
    echo "==================================================\n";
    foreach ($errors as $error) {
        echo "- $error\n";
    }
    echo "\n";
}

if ($successCount === count($models)) {
    echo "🎉 ALL MODELS TESTED SUCCESSFULLY!\n";
    echo "✅ All table mappings are correct and database connections work.\n";
} else {
    echo "⚠️  Some issues were found. Please review the errors above.\n";
}

echo "\n🏁 Model connection test completed!\n";

?>